﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WCFTrays.WCFServer;

namespace WCFTrays
{
    public partial class WCFInboxConfigControl : Form
    {
        private WCFInbox inbox;
        private DataTable queryParams;
        private BindingSource bindingDataTable = new BindingSource();
        public WCFInboxConfigControl(WCFInbox newInbox)
        {
            InitializeComponent();
            inbox = newInbox;
            textBoxName.Text = inbox._concreteName;
            textBoxConnString.Text = inbox.connString;
            textBoxHost.Text = inbox.hostUrl;
            textBoxMarkReadQuery.Text = inbox.markAsReadMessageQuery;
            textBoxSelectQuery.Text = inbox.selectMessageQuery;
            queryParams = new DataTable("Parameters");
            setupDataGridParameters();
           
        }

    

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (textBoxConnString.Text.Length == 0 || textBoxHost.Text.Length == 0 || textBoxMarkReadQuery.Text.Length == 0 || textBoxSelectQuery.Text.Length == 0 ||textBoxName.Text.Length == 0)
            {
                MessageBox.Show("Por favor complete todos los campos.", "Error de validación", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                inbox.connString = textBoxConnString.Text;
                inbox.hostUrl = textBoxHost.Text;
                inbox.selectMessageQuery = textBoxSelectQuery.Text;
                inbox.markAsReadMessageQuery = textBoxMarkReadQuery.Text;
                inbox.setConcreteInboxName(textBoxName.Text);
                inbox.markAsReadParameters = new List<WcfParameter>();
                foreach (DataGridViewRow row in dgParameters.Rows)
                {
                    if (row.Cells[0].Value != null && row.Cells[1].Value != null)
                    {
                        inbox.markAsReadParameters.Add(new WcfParameter()
                         {
                             ParameterName = row.Cells[0].Value.ToString().Trim(),
                             MatchingColumn = row.Cells[1].Value.ToString().Trim()
                         });
                    }
                }
                this.Dispose();
                
            }
            
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void setupDataGridParameters()
        {
            //dgParameters.AutoGenerateColumns = false;
            dgParameters.RowHeadersVisible = false;
            queryParams.Columns.Add("Parámetro");

            queryParams.Columns.Add("Columna");
            //Si la bandeja la abro por Ver propiedades, le cargo las parametros que ya existen
            if (inbox.markAsReadParameters != null)
            {
                foreach (WcfParameter p in inbox.markAsReadParameters)
                {
                    queryParams.Rows.Add(new string[] { p.ParameterName, p.MatchingColumn });
                }
            }

            bindingDataTable.DataSource = queryParams;
            dgParameters.DataSource = bindingDataTable;


            



           /* DataGridViewTextBoxColumn nameCol = new DataGridViewTextBoxColumn();
            nameCol.HeaderText = "Parámetro";
            DataGridViewTextBoxColumn realCol = new DataGridViewTextBoxColumn();
            realCol.HeaderText = "Columna";

            dgParameters.Columns.Insert(0, nameCol);
            dgParameters.Columns.Insert(1, realCol);*/
            dgParameters.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgParameters.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
           

        }

        private void dgParameters_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dgParameters.IsCurrentCellDirty)
            {
                dgParameters.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            string help = "Aqui deben ser ingresados los parámetros utilizados en la consulta y a que columna de la tabla corresponden.\n";
            help += "\n";
            help += "Por ejemplo, para la consulta: UPDATE Mensajes SET Procesados = 1 WHERE MensajeId = @ID\n";
            help += "\n";
            help += "El parámetro es @ID y la columna MensajeID\n";
            help += "\n";
            MessageBox.Show(this,help,"Ayuda",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
   
    }
}
